/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Form.Validation;

import com.lcfgly.CustomBean.PasswordChangeForm;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author lcfgly
 */
public class PasswordChangeValidation implements Validator{

    @Override
    public boolean supports(Class<?> clazz) {
        return PasswordChangeForm.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors, "oldpassword",null, "请填写原始密码");
        ValidationUtils.rejectIfEmpty(errors, "newpassword",null, "新密码不能为空");
        ValidationUtils.rejectIfEmpty(errors, "newpasswordagain",null, "请重复新密码");
        PasswordChangeForm form=(PasswordChangeForm)target;
        if(form.getNewpassword().length() < 6)
            errors.rejectValue("newpassword", null,"新密码长度不能小于6");
        if(!form.getNewpassword().equals(form.getNewpasswordagain()))
            errors.rejectValue("newpasswordagain",null, "请输入两次相同的新密码");
    }
    
}
